package com.org.litepal.slice;

import com.org.litepal.slice.aggregate.AggregateAbilitySlice;
import com.org.litepal.slice.crud.CrudAbilitySlice;
import com.org.litepal.slice.manager.ManagerAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import org.litepal.harmony.ResourceTable;

/**
 * 主页
 */
public class MainAbilitySlice extends AbilitySlice {
    /**
     * 管理表
     */
    private static final int MANAGER = 0;
    /**
     * 增删查改
     */
    private static final int CRUD = 1;
    /**
     * 聚合函数
     */
    private static final int AGGREGATE = 2;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //管理表
        Button manager = (Button) findComponentById(ResourceTable.Id_btnMain_manager);
        manager.setClickedListener(component -> startAbilityManager(MANAGER));

        //增删查改
        Button crud = (Button) findComponentById(ResourceTable.Id_btnMain_crud);
        crud.setClickedListener(component -> startAbilityManager(CRUD));

        //聚合函数
        Button aggregate = (Button) findComponentById(ResourceTable.Id_btnMain_aggregate);
        aggregate.setClickedListener(component -> startAbilityManager(AGGREGATE));
    }

    private void startAbilityManager(int index) {
        AbilitySlice slice = null;
        switch (index) {
            case MANAGER:
                slice = new ManagerAbilitySlice();
                break;
            case CRUD:
                slice = new CrudAbilitySlice();
                break;
            case AGGREGATE:
                slice = new AggregateAbilitySlice();
                break;
            default:
                break;
        }
        if (slice == null) {
            return;
        }
        present(slice, new Intent());
    }
}